package com.tjyx.maintain.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.tjyx.common.util.db.DBConnector;
import com.tjyx.maintain.dto.News;


public class NewDao {
	private Connection connection;
	
	public NewDao(){
		connection=DBConnector.getConnection();
	}

    public void addNews(String newsTitle,String newsContent,int newsClassId,int hit,String createPerson,java.sql.Date addTime,String isOrign,String newsPic) throws SQLException{
     	String sql="insert into eps_news(newsTitle,newsContent,newsClassId,hitNumber,createPerson,addTime,isOrign,newsPic) values(?,?,?,?,?,?,?,?)";
     	PreparedStatement statement=connection.prepareStatement(sql);
    	statement.setString(1,newsTitle);
    	statement.setString(2,newsContent);
    	statement.setInt(3,newsClassId);
    	statement.setInt(4,hit);
    	statement.setString(5,createPerson);
    	statement.setDate(6,addTime);
    	statement.setString(7,isOrign);
    	statement.setString(8,newsPic);
    	statement.execute();
    }

	public News queryById(int id) throws SQLException {
		String sql="select * from eps_news where id=?";
		PreparedStatement statement=connection.prepareStatement(sql);
		statement.setInt(1, id);
		ResultSet rs=statement.executeQuery();
		News news=new News();
		while(rs.next()){
			news.setId(rs.getInt("id"));
			news.setNewsTitle(rs.getString("newsTitle"));
			news.setNewsContent(rs.getString("newsContent"));
			news.setNewsPic(rs.getString("newsPic"));
			news.setNewsClassId(rs.getInt("newsClassId"));
			news.setAddTime(rs.getDate("addTime").toString());
			news.setHitNumber(rs.getInt("hitNumber"));
			news.setIsChecked(rs.getInt("isChecked"));
			news.setCreatePerson(rs.getString("createPerson"));
			news.setIsOrign(rs.getString("isOrign"));
		}
		return news;
	}	
}
